package com.cs;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

/**
 * 服务端
 */
public class EchoServer {
    public static void main(String[] args) {
        // 1.启动服务
        ServerSocket listenSocket = null;
        Socket clientSocket = null;
        BufferedReader in = null;
        BufferedWriter out = null;
        try {
            listenSocket = new ServerSocket();
            SocketAddress serverAddr = new InetSocketAddress("localhost", 5000);
            listenSocket.bind(serverAddr);
            System.out.println("1.服务器启动成功! 开始在 localhost 的 5000 端口侦听连接... ");

            // 2.处理连接
            clientSocket = listenSocket.accept();
            System.out.println("2.客户端连接成功! 客户机地址和端口: " + clientSocket.getRemoteSocketAddress());
            // 3.与客户机会话
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
            // 从客户机接收字符串
            String recvStr = in.readLine();
            System.out.println("3.1 服务器接收到字符串: " + recvStr);
            // 向客户机回送字符串
            out.write(recvStr);
            out.newLine();
            out.flush();
            System.out.println("3.2 服务器回送字符串成功: " + recvStr);
        } catch (IOException e) {
            System.out.println("异常信息: " + e.getMessage());
        }
        // 4. 关闭套接字和流
        try {
            if (in != null) in.close();
            if (out != null) out.close();
            if (listenSocket != null) listenSocket.close();
            if (clientSocket != null) clientSocket.close();
            System.out.println("4. 关闭套接字和流成功!");
        } catch (IOException e) {
            System.out.println("异常信息 " + e.getMessage());
        }
    }
}
